package org.gwtEss.portlet;

import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Widget;

public class PortletContainer extends AbsolutePanel{
//	PortletCol[] portletCols;
//	AbsolutePanel ap = new AbsolutePanel();
	private int hPadding = 10;
	public void addPortlet(int col, Widget w, Widget titleWidget){
		PortletCol pc = portletCols[col];
		
		PortletImpl p = new PortletImpl(col);
		p.setBodyWidget(w);
		if (titleWidget != null)
		p.setTitleWidget(titleWidget);
		p.setWidth(widthPerCol + "px");

		pc.addPortlet((Widget)p);
	}
	public void addPortlet(int col, Widget w){
		addPortlet(col, w, null);
	}
	private int placeHolderCol = -1, placeHolderCell = -1; 
	
	/*private int findBelongingColumn(int left){
		int suggestedCol = Math.round(left/widthPerCol);
		return suggestedCol > numOfCols - 1? numOfCols - 1: suggestedCol;
	}*/

	private int findBelongingColumn(int left){
		int suggestedCol = (int)Math.round((float)left/widthPerCol + 0.1);
		return suggestedCol > numOfCols - 1? numOfCols - 1: suggestedCol;
	}
	
	private class PortletImpl extends Portlet{

		private int col;

		public PortletImpl(int col) {
			this.col = col;
		}
		private void doDrag(){
			int belongingCol = findBelongingColumn(this.getPopupLeft());
			PortletCol containerBelongingCol = portletCols[belongingCol];
//			int belongingCell = containerBelongingCol.findBelongingCell(this.getPopupTop() + this.getOffsetHeight()/2);
			int belongingCell = containerBelongingCol.findBelongingCell(this);

//			System.out.println("belongingCol: " + belongingCol + ", belongingCell: " + belongingCell );
//			System.out.println("placeHolderCol: " + placeHolderCol + ", placeHolderCell: " + placeHolderCell );
			
			if (placeHolderCol == belongingCol && placeHolderCell == belongingCell) return;
			if (belongingCell == -1) belongingCell = placeHolderCell;

			
			PortletCol oldContainerPlaceHolderCol = portletCols[placeHolderCol];
			
//			oldContainerPlaceHolderCol.remove(placeHolder);
//			containerBelongingCol.add(belongingCell, placeHolder);
			
			if (oldContainerPlaceHolderCol != containerBelongingCol || containerBelongingCol.indexOf(placeHolder) != belongingCell){
				oldContainerPlaceHolderCol.remove(placeHolder);
				containerBelongingCol.add(belongingCell, placeHolder);
			}/*else {
				if (containerBelongingCol.indexOf(placeHolder) != belongingCell){
					oldContainerPlaceHolderCol.remove(placeHolder);
					oldContainerPlaceHolderCol.add(belongingCell, placeHolder);
				}
			}*/
			
			placeHolderCell = belongingCell; 
			placeHolderCol = belongingCol;				
		}
		
		@Override
		public void onDrag(int mouseX, int mouseY) {
			doDrag();
		}
		@Override
		public void onDragEnd(int mouseX, int mouseY) {
			doDrag();
			portletCols[placeHolderCol].replace(placeHolder, this);
			placeHolder.setVisible(false);
			col = placeHolderCol;
			placeHolderCol = -1;
			placeHolderCell = -1;
//			removeStyleName("transbox");
			
		}

		@Override
		public void onDragStart(int mouseX, int mouseY) {
			placeHolder.setVisible(true);
			placeHolder.setHeight(this.getOffsetHeight() + "px");
			PortletCol portletCol = portletCols[col];
			placeHolderCell = portletCol.indexOf(this);
			portletCol.remove(this);
			placeHolderCol = col;
			portletCols[placeHolderCol].add(placeHolderCell, placeHolder);
//			addStyleName("transbox");
//			doDrag();
		}
	}
	public PortletContainer(int numOfCols, int widthPerCol) {

		setSize("100%", "100%");
		this.numOfCols = numOfCols;
		this.widthPerCol = widthPerCol;
		portletCols = new PortletCol[numOfCols];
		int widthSoFar = 0; 
		for (int i=0; i<numOfCols; i++){
			portletCols[i] = new PortletCol(this, widthSoFar , widthPerCol);
			widthSoFar += widthPerCol + hPadding;
		}
		add(placeHolder);

		placeHolder.setWidth(widthPerCol + "px");
		placeHolder.setVisible(false);
	}
	PortletCol[] portletCols;
	int numOfCols;
	int widthPerCol;
	DropHereWidget placeHolder = new DropHereWidget();
}
